package com.gosophia.govDoc.action.turning;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import net.sf.json.JSONArray;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.ws.TaskManagementWebService;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.ws.UserManagementWebService;

@Scope("prototype")
@Controller("GetDocUserInfoAllAction")
public class GetDocUserInfoAllAction extends BaseSupportAction{

	private static final long serialVersionUID = 188780888656710659L;
	
	private Long jobId;
	
	@Autowired
	private UserManagementWebService userManagementWebService;

	@Autowired
	private TaskManagementWebService taskManagementWebService;
	
	private List<UserInfoDTO> userInfoDTO = new ArrayList<UserInfoDTO>();
	
	@Override
	public String _execute() throws Exception {
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
        .getContext().getAuthentication().getPrincipal();
		Long userInfoId = userDetails.getUserAccountDTO().getUserInfoDTO().getUserInfoId();
		Long simulatorId = null;
		if (jobId != null) {
			simulatorId = taskManagementWebService
					.findJobDetailByJobId(jobId).getMissionDTO()
					.getUndertakerDTO().getUserInfoId();

		}
		List<UserInfoDTO> userInfoDTOs = userManagementWebService.findAllUserInfoHasAccount();
		for(UserInfoDTO u:userInfoDTOs){
			if(!u.getUserInfoId().equals(userInfoId)){
				if(jobId != null ){
					if(!u.getUserInfoId().equals(simulatorId)){
						userInfoDTO.add(u);
					}
				}else{
					userInfoDTO.add(u);
				}
			}
		}
		return SUCCESS;
	}

	public List<UserInfoDTO> getUserInfoDTO() {
		return userInfoDTO;
	}

	public void setUserInfoDTO(List<UserInfoDTO> userInfoDTO) {
		this.userInfoDTO = userInfoDTO;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public Long getJobId() {
		return jobId;
	}

    	
}
